const fs = require('fs')
// 为了解决回调地狱,resolve,reject也是方法。resolve和reject是形参，可以随意写

let promise1 = new Promise((resolve, reject) => {
    fs.readFile('test1.txt', 'utf8', (error, data) => {
        if (error != null) {
            /* console.log(error);
            return */
            // 用promise就这么写。交给catch方法处理
            reject(error)
        }
        console.log('读取成功！');
        resolve(data)
    })
})


let promise2 = new Promise((resolve, reject) => {
    fs.readFile('test2.txt', 'utf8', (error, data) => {
        if (error != null) {
            /* console.log(error);
            return */
            // 用promise就这么写。交给catch方法处理
            reject(error)
        }
        console.log('读取成功！');
        resolve(data)
    })
})


let promise3 = new Promise((resolve, reject) => {
    fs.readFile('test3.txt', 'utf8', (error, data) => {
        if (error != null) {
            /* console.log(error);
            return */
            // 用promise就这么写。交给catch方法处理
            reject(error)
        }
        console.log('读取成功！');
        resolve(data)
    })
})

// 这个也是形参，也可以随意写
promise1.then((res1) => {
    console.log('res1', res1);
    return promise2
}).catch(err => {
    console.log(err);
})
    .then(res2 => {
        console.log('res2', res2);
        return promise3
    })
    .then(res3 => {
        console.log('res3', res3);

    })
